package net.skyedu.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.skyedu.bean.StuFeeDetail;
import net.skyedu.bean.StuFeeSecond;
import net.skyedu.dao.AjaxDAO;

public class AlreadyFeeList extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
	
//		AjaxDAO ajaxDAO = new AjaxDAO();
		
		int studentId = Integer.parseInt(request.getParameter("stuId"));
		int SubjectId = Integer.parseInt(request.getParameter("subClassType"));
		int PeriodId = Integer.parseInt(request.getParameter("terId"));
		int gradId = Integer.parseInt(request.getParameter("graId"));
		int courseid = Integer.parseInt(request.getParameter("courseid"));
		
		int currentpage = Integer.parseInt(request.getParameter("currentPage"));	
  		int pagesize = Integer.parseInt(request.getParameter("pageSize"));		
//		JSONArray json = ajaxDAO.ActualFee(studentId, SubjectId, GradeId, PeriodId, startIndex, pagesize);
		int startIndex=0;
		List<StuFeeSecond> stufeeList=new ArrayList<StuFeeSecond>();
		StringBuffer jsonAttr = new StringBuffer("[");
		try{
	    	  startIndex = (currentpage - 1) * pagesize;
	    	  AjaxDAO ajaxDAO = new AjaxDAO();
		      int pageCount = ajaxDAO.ActualFeeList(studentId, SubjectId, gradId, PeriodId, startIndex, pagesize,0,courseid).size();
		      if(pageCount != 0){
		    	  stufeeList = ajaxDAO.ActualFeeList(studentId, SubjectId, gradId, PeriodId, startIndex, pagesize,1,courseid);
		    	  for(int i = 0; i < stufeeList.size(); i++){
		    		  StuFeeSecond att = stufeeList.get(i);
			    	  jsonAttr.append("{");
			    	  jsonAttr.append("\"stuName\":\""+att.getStuName()+"\",");
			    	  jsonAttr.append("\"claName\":\""+att.getClaName()+"\",");
			    	  jsonAttr.append("\"claNum\":\""+att.getAlreadyClaNum()+"\",");
			    	  jsonAttr.append("\"unitfee\":\""+att.getUnitfee()+"\",");
			    	  jsonAttr.append("\"feesum\":\""+att.getAlreadyFee()+"\",");	
			    	  jsonAttr.append("\"discount\":\""+att.getDiscount()+"\",");		    	  
			    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
			    	  jsonAttr.append("}");
			    	  if(i<stufeeList.size()-1){
			    		  jsonAttr.append(",");
			    	  }
			       }
		    	  
			      
		      }else{
		    	  jsonAttr.append("{");
		    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); 
		    	  jsonAttr.append("}");
		      }
		      jsonAttr.append("]");
		    PrintWriter out = response.getWriter();
		    out.print(jsonAttr);
		    System.out.println(jsonAttr);
		    out.flush();
		    out.close();
		    
	      }catch(Exception e){
	    	  //写到日志表中
	    	  e.printStackTrace();
	      }
		
	}

}
